//
//  ViewController.swift
//  silideTest
//
//  Created by AINI on 2019/2/11.
//  Copyright © 2019 AINI. All rights reserved.
//

import UIKit

class ViewController: UIViewController {
    var targetValue :Int = 0;
    var currentValue :Int = 0;
    var score :Int = 0;
    var round :Int = 1;
    @IBOutlet weak var name: UILabel!
    @IBOutlet weak var hitBtn: UIButton!
    @IBOutlet weak var count: UILabel!
    @IBOutlet weak var sildeBar: UISlider!
    @IBOutlet weak var again: UIButton!
    @IBOutlet weak var scorelabel: UILabel!
    @IBOutlet weak var roundLabel: UILabel!
    override func viewDidLoad() {
        super.viewDidLoad()
        currentValue = lroundf(sildeBar!.value)
        setTarget()
        updateLabel()
        let thumbImageNormal = UIImage(named: "SliderThumb-Normal")!
        sildeBar.setThumbImage(thumbImageNormal, for: .normal)
        let  thumbHighlighted = UIImage(named: "SliderThumb-Highlighted")!
        sildeBar.setThumbImage(thumbHighlighted, for: .highlighted)
        let insets = UIEdgeInsets(top: 0, left: 14, bottom: 0, right: 14)
        let  trackLeftImage = UIImage(named: "SliderTrackLeft")!
        let trackLeftResizable = trackLeftImage.resizableImage(withCapInsets: insets)
        sildeBar.setMinimumTrackImage(trackLeftResizable, for: .normal)
        
        let  trackRightImage = UIImage(named: "SliderTrackRight")!
        let trackRightResizable = trackRightImage.resizableImage(withCapInsets: insets)
        sildeBar.setMaximumTrackImage(trackRightResizable, for: .normal)
        
        // Do any additional setup after loading the view, typically from a nib.
    }


    @IBAction func btnTip(_ sender: Any) {
        currentValue = lroundf(sildeBar!.value)
        showAlert()
    }
    @IBAction func showAlert() {
        let point = 100-abs(currentValue-targetValue);
        let title = point<80 ? "怎么回事，要加油哦" : point>80&&point<90 ? "很接近了":"完美"
        let message = "获取分数\(point)，"
        score += point;
        round+=1;
        let alert = UIAlertController(title: title, message: message, preferredStyle: .alert)
        let action = UIAlertAction(title: "OK", style: .default, handler: nil)
        alert.addAction(action)
        present(alert,animated: true,completion: nil)
        setTarget()
        updateLabel()
    }

    @IBAction func again(_ sender: Any) {
        score = 0;
        round = 1;
        setTarget()
        updateLabel()
    }
    func random(min:Int,max:Int) -> Int {
        return Int.randomIntNumber(lower: min, upper: max)
    }
    func setTarget(){
        targetValue = random(min: 0, max: 100)
    }
    func updateLabel() {
        count.text = String(targetValue)
        scorelabel.text = String(score)
        roundLabel.text = String(round)
    }
}

public extension Int {
    /*这是一个内置函数
     lower : 内置为 0，可根据自己要获取的随机数进行修改。
     upper : 内置为 UInt32.max 的最大值，这里防止转化越界，造成的崩溃。
     返回的结果： [lower,upper) 之间的半开半闭区间的数。
     */
    public static func randomIntNumber(lower: Int = 0,upper: Int = Int(UInt32.max)) -> Int {
        return lower + Int(arc4random_uniform(UInt32(upper - lower)))
    }
    /**
     生成某个区间的随机数
     */
    public static func randomIntNumber(range: Range<Int>) -> Int {
        return randomIntNumber(lower: range.lowerBound, upper: range.upperBound)
    }
}


